package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.reimburse.dao.BasePayItemDao;
import com.open.capacity.reimburse.dao.BasePayItemFormSelectDao;
import com.open.capacity.reimburse.entity.BasePayItem;
import com.open.capacity.reimburse.entity.BasePayItemFormSelect;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.user.model.SysArea;
import com.open.capacity.utils.SysUserUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.codehaus.jackson.map.Serializers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.*;
import java.util.stream.Collectors;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.BasePayItemForm;
import com.open.capacity.reimburse.dao.BasePayItemFormDao;
import com.open.capacity.reimburse.service.BasePayItemFormService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;


@Service
public class BasePayItemFormServiceImpl implements BasePayItemFormService {

    @Autowired
    private BasePayItemDao basePayItemDao;

    @Autowired
    private BasePayItemFormDao basePayItemFormDao;

    @Autowired
    private BasePayItemFormSelectDao formSelectDao;

    /**
     * 添加
     *
     * @param basePayItemForm
     */
    @Transactional
    public int save(BasePayItemForm basePayItemForm) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();

        String payitemFormId = UUIDUtils.getOriginalUUIDWithoutBar();
        basePayItemForm.setId(payitemFormId);
        basePayItemForm.setStatus(1);
        basePayItemForm.setUpdateUserid(loginUser.getId());
        basePayItemForm.setUpdateUsername(loginUser.getRealName());
        basePayItemForm.setUpdateTime(new Date());

        basePayItemFormDao.save(basePayItemForm);

        if (basePayItemForm.getDataType().equals("select")) {
            Assert.isTrue(CollectionUtils.isNotEmpty(basePayItemForm.getSelectList()), "数据类型为[选择]，请填写选择项");
            for (BasePayItemFormSelect item : basePayItemForm.getSelectList()) {
                item.setId(UUIDUtils.getOriginalUUIDWithoutBar());
                item.setPayitemFormId(payitemFormId);
                item.setPayitemId(basePayItemForm.getPayitemId());
                item.setStatus(1);
                item.setUpdateUserid(loginUser.getId());
                item.setUpdateUsername(loginUser.getRealName());
                item.setUpdateTime(new Date());
                formSelectDao.save(item);
            }
        }

        return 1;
    }

    /**
     * 修改
     *
     * @param basePayItemForm
     */
    @Transactional
    public int update(BasePayItemForm basePayItemForm) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();

        basePayItemForm.setUpdateUserid(loginUser.getId());
        basePayItemForm.setUpdateUsername(loginUser.getRealName());
        basePayItemForm.setUpdateTime(new Date());
        basePayItemFormDao.update(basePayItemForm);

        if (basePayItemForm.getDataType().equals("select")) {
            Assert.isTrue(CollectionUtils.isNotEmpty(basePayItemForm.getSelectList()), "数据类型为[选择]，请填写选择项");
            formSelectDao.deleteByFormId(basePayItemForm.getId());
            for (BasePayItemFormSelect item : basePayItemForm.getSelectList()) {
                item.setId(UUIDUtils.getOriginalUUIDWithoutBar());
                item.setPayitemFormId(basePayItemForm.getId());
                item.setPayitemId(basePayItemForm.getPayitemId());
                item.setStatus(1);
                item.setUpdateUserid(loginUser.getId());
                item.setUpdateUsername(loginUser.getRealName());
                item.setUpdateTime(new Date());
                formSelectDao.save(item);
            }
        }
        return 1;
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return basePayItemFormDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public BasePayItemForm findById(String id) {
        BasePayItemForm form = basePayItemFormDao.findById(id);
        List<BasePayItemFormSelect> selectList = formSelectDao.findByFormId(id, null);
        form.setSelectList(selectList);
        return form;
    }


    /**
     * 列表
     *
     * @param payitemIds
     * @return
     */
    public Map<String, List<BasePayItemForm>> findByPayitemIds(String payitemIds) {
        String[] idArrays = payitemIds.split(",");

        Map<String, List<BasePayItemForm>> map = new HashMap<>();
        for (String id : idArrays) {
            BasePayItem payitem = basePayItemDao.findById(id);
            List<BasePayItemForm> forms = basePayItemFormDao.findByPayitemId(id);
            if (CollectionUtils.isNotEmpty(forms)) {
                forms.forEach(f -> {
                    f.setSelectList(formSelectDao.findByFormId(f.getId(), null));
                    f.setBillSort(0);
                });
            }
            map.put(payitem.getId() + "," + payitem.getName(), forms);
        }
        return map;
    }

    @Override
    public List<BasePayItemFormSelect> findSelectByFormId(String formId, String keyword) {
        return formSelectDao.findByFormId(formId, keyword);
    }

    /**
     * 列表
     *
     * @param payitemId
     * @return
     */
    public List<BasePayItemForm> findByPayitemId(String payitemId) {
        return basePayItemFormDao.findByPayitemId(payitemId);
    }

    @Override
    public BasePayItemFormSelect findSelectById(String selectId) {
        return formSelectDao.findById(selectId);
    }

}
